<?php

namespace App\Http\Controllers\Admin;

use App\Services\Wxtj\WxtjInterface;
use Illuminate\Http\Request;

/**
 * 微信接口分析统计
 * @package App\Http\Controllers\Admin
 */
class WxtjInterfaceController extends BaseWxController
{
    /**
     * 接口分析数据
     * @param Request $request
     */
    public function summary(Request $request, WxtjInterface $wxtjInterface)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjInterfaceSummary::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $endTime = $request->get('end_time', '');
            $endTime = $endTime ? date('Y-m-d', strtotime($endTime)) : date('Y-m-d', strtotime('-1 days'));
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('ref_date', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("ref_date >= '" . $startTime . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("ref_date <= '" . $endTime . "'");
            }
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByDate($rows, 'ref_date', ['callback_count','fail_count','total_time_cost','max_time_cost'], $startTime, $endTime);
            return $formatData;
        }
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-7 days'));
        $this->viewData['endDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjinterface.summary', $this->viewData);
    }

    /**
     * 接口分析(分时)数据
     * @param Request $request
     */
    public function summaryhour(Request $request, WxtjInterface $wxtjInterface)
    {
        if ($request->ajax()) {
            //关注渠道
            $query = \App\Models\WxtjInterfaceSummaryhour::select('*');
            //查询指定微信应用
            $curWxMp = \Auth::user()->getWxMpCurrent();
            $appid = $curWxMp ? $curWxMp['appid'] : '';
            $query->where('appid', $appid);
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $startTime = $startTime ? date('Y-m-d', strtotime($startTime)) : date('Y-m-d', strtotime('-7 days'));
            $query->where('ref_date', $startTime);
            $rows = $query->get();
            $rows = $rows ? $rows->toArray() : [];
            $formatData = formatChartDataByHour($rows, 'ref_hour', ['callback_count','fail_count','total_time_cost','max_time_cost']);
            return $formatData;
        }
        $this->viewData['startDate'] = date('Y-m-d', strtotime('-1 days'));
        return view('admin.wxtjinterface.summaryhour', $this->viewData);
    }
}
